package com.example.liyun.joy;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.liyun.joy.serviceutils.JokeBean;
import com.example.liyun.joy.serviceutils.NewsBean;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static android.content.Intent.getIntent;

public class MyRecycleAdapter extends RecyclerView.Adapter<MyRecycleAdapter.ViewHolder>{
    private LayoutInflater minflater;
    private Map<String,List> mData;
    List<JokeBean.ResultBean.DataBean> jokeDatas = new ArrayList<>();
    List<NewsBean.ResultBean.DataBean> newsDatas=new ArrayList<>();
    public String type;
    public Context context;
    //为接口创建一个成员变量的点击监听器



    public MyRecycleAdapter(Context context,String type){
        this.context=context;
        this.minflater=LayoutInflater.from(context);
        this.type=type;
        notifyDataSetChanged();

    }

    public void setJoke(List<JokeBean.ResultBean.DataBean> Datas){
        jokeDatas.addAll(Datas);
        Log.i("Size","jokeDatas"+jokeDatas.size());
        notifyDataSetChanged();
    }
    public void setNews(List<NewsBean.ResultBean.DataBean> Datas){
        newsDatas.addAll(Datas);
        notifyDataSetChanged();
    }



    /**
     * item显示类型
     * 在这里为每个item添加点击事件
     * @param viewGroup
     * @param i
     * @return
     */
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View newsView=minflater.inflate(R.layout.recycleview_item_news,viewGroup,false);
        View jokeView=minflater.inflate(R.layout.recycleview_item_joke,viewGroup,false);
        //将position保存到news_title的Tag中，以便点击时获取
        /*newsView.setTag(i);
        Log.i("itemUrl", String.valueOf(i));*/

        ViewHolder viewHolder=new ViewHolder(type.equals("joke")?jokeView:newsView);


        return viewHolder;
    }

    /**
     * 数据绑定于Item显示
     *
     * @param viewHolder
     * @param i
     */
    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        if (type.equals("joke")){
            viewHolder.joke_content.setText(jokeDatas.get(i).getContent());
            viewHolder.joke_time.setText(jokeDatas.get(i).getUpdatetime());
        }else {
            //使用Glide库来加载网络图片显示在Image View上
            Glide.with(context).load(newsDatas.get(i).getThumbnail_pic_s()).into(viewHolder.news_icon);
            viewHolder.news_title.setText(newsDatas.get(i).getTitle());
            viewHolder.news_time.setText(newsDatas.get(i).getDate());

            /**
             * 将position保存到news_title的Tag中，以便点击时获取.
             * 这里需要特别注意下。一定得在onBindViewHolder上保存position
             */
            viewHolder.itemView.setTag(i);
        }

    }


    /**
     * 返回数据长度
     * @return
     */
    @Override
    public int getItemCount() {
        return type.equals("joke")?jokeDatas.size():newsDatas.size();
    }
    /**
     * 自定义ViewHolder，持有Item内所有的界面元素（子View），
     *
     */
    public class ViewHolder extends RecyclerView.ViewHolder{
        public final ImageView news_icon;
        public TextView news_title,news_time;
        public TextView joke_content,joke_time;
        public View itemView;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            this.itemView=itemView;
            news_icon=itemView.findViewById(R.id.iv_icon);
            news_title=itemView.findViewById(R.id.tv_title);
            news_time=itemView.findViewById(R.id.tv_time);
            joke_content=itemView.findViewById(R.id.joke_content);
            joke_time=itemView.findViewById(R.id.joke_time);
            if (!type.equals("joke")){
                //为每个item设置点击监听
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //获取之前保存的position
                        int position= (int) v.getTag();
                        Intent searchIntent=new Intent(context,SearchActivity.class);
                        searchIntent.putExtra("url",newsDatas.get(position).getUrl());
                        Log.i("itemUrl",newsDatas.get(position).getUrl()+position);
                        context.startActivity(searchIntent);
                    }
                });
            }
        }
    }

}
